﻿using ConfExpert.Forms.Base;
using DevTeam;

using System;
namespace ConfExpert.Forms.Araclar
{
    public partial class MyCustomers : XForm
    {
        public MyCustomers()
        {
            InitializeComponent();
        }

        bool tl = false;
        private void MyCustomers_Load(object sender, EventArgs e)
        {
            gvCustomer.DisableRowDelete();
            gvBrand.DisableRowDelete();
            gvCustomer.Init("GetMyCustomers", WhoAmI);
            gvCustomer.AddDetailGrid(gvBrand, "CustomerId", 1);
            gvBrand.Columns["BrandId"].ColumnEdit = null;
            gvBrand.SetCaptions("BrandId:#", "Name:Marka");
            gvBrand.MakeColumnsReadOnly("BrandId");
            gvBrand.Columns["Deleted"].VisibleIndex = 100;
            gvBrand.SetCaptions("Deleted:Silindi");

            tl = gvCustomer.Dataset().Tables[2].Rows[0]["teamleader"].ToInt(-1) > 0;

            gvCustomer.ClearColumnEdit("CustomerId");
            gvCustomer.MakeColumnsReadOnly("CustomerId");
            gvCustomer.MakeColumnInvisible("CariId");
            gvCustomer.Columns["Deleted"].VisibleIndex = 100;
            gvCustomer.SetCaptions("Deleted:Silindi");

            if (!tl)
            {
                gvBrand.MakeReadOnly();
                gvCustomer.MakeReadOnly("Username", "Password");
                btnEkle.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            }
            else
            {
                btnEkle.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                btnEkle.ItemClick += btnEkle_ItemClick;
            }

            splitContainerControl1.AutoSize(0.65);
            btnKaydet.ItemClick += btnKaydet_ItemClick;
            //ExpandCollapse(splitContainerControl1, AcKapa.Collapse);
            gvCustomer.BestFitColumns();
            gvBrand.BestFitColumns();

            if (gvCustomer.Columns["Sorumlu"].Width > 150)
                gvCustomer.Columns["Sorumlu"].Width = 150;

            gvBrand.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;


        }

        void btnEkle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (YeniMusteri ym = new YeniMusteri())
            {
                if (ym.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    if (ym.CustomerId > 0)
                    {
                        int handle = gvCustomer.LocateByValue("CustomerId", ym.CustomerId);
                        gvCustomer.FocusedRowHandle = handle;
                        gvCustomer.MakeRowVisible(handle);
                    }
                    else if (ym.CariId > 0)
                    {
                        gvCustomer.RefreshDataSource("InsYeniMusteri", ym.CariId, ym.str, WhoAmI);
                        int handle = gvCustomer.LocateByValue("CariId", ym.CariId);
                        gvCustomer.FocusedRowHandle = handle;
                        gvCustomer.MakeRowVisible(handle);
                    }
                    else if (ym.str != string.Empty)
                    {
                        gvCustomer.RefreshDataSource("InsYeniMusteri", ym.CariId, ym.str, WhoAmI);
                        gvCustomer.MoveLast();
                    }
            }
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvCustomer.EndEditing();

            gvCustomer.Update("Deleted,CustomerId,Name,CountryId,TakimId,Username,Password,PrfPaymentTermId,PrfShipMethodId", "Customer");
            gvBrand.Update();
        }
    }
}